package com.lanou.wuyou.gift.category;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.wuyou.gift.R;
import com.lanou.wuyou.gift.bean.CategoryDownBean;
import com.lanou.wuyou.gift.bean.CategoryTopBean;
import com.squareup.picasso.Picasso;

/**
 * Created by dllo on 17/3/17.
 */

public class CateListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    private Context context;
    private CategoryDownBean categoryDownBean;
    private CategoryTopBean categoryTopBean;

    public void setCategoryDownBean(CategoryDownBean categoryDownBean) {
        this.categoryDownBean = categoryDownBean;
        notifyDataSetChanged();
    }

    public CateListAdapter(Context context) {
        this.context = context;
    }

    public void setCategoryTopBean(CategoryTopBean categoryTopBean) {
        this.categoryTopBean = categoryTopBean;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case 0:
                View topView = LayoutInflater.from(context).inflate(R.layout.item_category_recycler,parent,false);
                holder = new TopHodler(topView);
                break;
            case 1:
                View downView = LayoutInflater.from(context).inflate(R.layout.item_category_recycler_view,parent,false);
                holder = new DownHolder(downView);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        switch (getItemViewType(position)){
            case 0:
                TopHodler topHodler = (TopHodler) holder;
                GridLayoutManager gridLayoutManagerg = new GridLayoutManager(context,3, LinearLayoutManager.HORIZONTAL,false);
                CateListTopAdapter adapter = new CateListTopAdapter(context);
                topHodler.recyclerView.setLayoutManager(gridLayoutManagerg);
                adapter.setBean(categoryTopBean);
                topHodler.recyclerView.setAdapter(adapter);
                break;
            case 1:
                DownHolder downHolder = (DownHolder) holder;
        if (categoryDownBean != null){
            CategoryDownBean.DataBean.ChannelGroupsBean channelGroupsBean = categoryDownBean.getData().getChannel_groups().get(position - 1);
            if (channelGroupsBean != null){
                downHolder.titleTv.setText(channelGroupsBean.getName());
            }
            CategoryDownBean.DataBean.ChannelGroupsBean channelsBean =
                    categoryDownBean.getData().getChannel_groups().get(position - 1);
            if (channelsBean != null){
                Picasso.with(context).load(channelsBean.getChannels().get(0).getCover_image_url()).into(downHolder.view);
                Picasso.with(context).load(channelsBean.getChannels().get(1).getCover_image_url()).into(downHolder.view1);
                Picasso.with(context).load(channelsBean.getChannels().get(2).getCover_image_url()).into(downHolder.view2);
                Picasso.with(context).load(channelsBean.getChannels().get(3).getCover_image_url()).into(downHolder.view3);
                Picasso.with(context).load(channelsBean.getChannels().get(4).getCover_image_url()).into(downHolder.view4);
                Picasso.with(context).load(channelsBean.getChannels().get(5).getCover_image_url()).into(downHolder.view5);
                downHolder.view.setAdjustViewBounds(true);
                downHolder.view1.setAdjustViewBounds(true);
                downHolder.view2.setAdjustViewBounds(true);
                downHolder.view3.setAdjustViewBounds(true);
                downHolder.view4.setAdjustViewBounds(true);
                downHolder.view5.setAdjustViewBounds(true);
            }
        }
                break;
        }
    }

    @Override
    public int getItemCount() {
        if (categoryDownBean == null){
            return 0;
        }
        if (categoryDownBean.getData() == null){
            return 0;
        }
        return categoryDownBean.getData().getChannel_groups() != null && categoryDownBean.getData().getChannel_groups().size() > 0 ?
                categoryDownBean.getData().getChannel_groups().size() + 1 : 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0){
            return 0;
        }else {
            return 1;
        }
    }

    class TopHodler extends RecyclerView.ViewHolder{

        RecyclerView recyclerView;
        public TopHodler(View itemView) {
            super(itemView);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.recycler_view_category_top);
        }
    }
    class DownHolder extends RecyclerView.ViewHolder {
        ImageView view,view1,view2,view3,view4,view5;
        TextView titleTv;
        public DownHolder(View itemView) {
            super(itemView);
            view = (ImageView) itemView.findViewById(R.id.view_category_recycler);
            view1 = (ImageView) itemView.findViewById(R.id.view_category_recycler1);
            view2= (ImageView) itemView.findViewById(R.id.view_category_recycler2);
            view3 = (ImageView) itemView.findViewById(R.id.view_category_recycler3);
            view4 = (ImageView) itemView.findViewById(R.id.view_category_recycler4);
            view5 = (ImageView) itemView.findViewById(R.id.view_category_recycler5);
            titleTv = (TextView) itemView.findViewById(R.id.title_category_recycler);
        }
    }
}
